import redis
from flask import Flask, session
from flask_migrate import MigrateCommand, Migrate
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
from flask_session import Session

app = Flask(__name__)

class Config(object):
    '''工程配置信息'''
    DEBUG = True
    SECRET_KEY = "NF4/1bi+vlFBI57bu1X3YD/tbbQjYKEjOJ1pBnmM+kuOwKnoG+yFS9+BzlECFYDo"

    # 配置数据库
    SQLALCHEMY_DATABASE_URI = "mysql://root:mysql@127.0.0.1:3306/information36"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # 配置redis
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 6379

    # flask_session 的配置信息
    SESSION_TYPE = "redis" # 指定session 保存到redis中
    SESSION_USE_SIGNER = True # 让cookie中的session_id被加密签名处理
    SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT) # 使用redis的实例
    PERMANENT_SESSION_LIFETIME = 86400 # session的有效期,单位是秒



app.config.from_object(Config)
db = SQLAlchemy(app)

redis_store = redis.StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT)

# 配置CSRF
CSRFProtect(app)

@app.route('/')
def index():
    session["name"] = "itheima"
    return 'index22222'

manager = Manager(app)
Migrate(app, db)
manager.add_command("db", MigrateCommand)

if __name__ == '__main__':
    manager.run()